#include "semaphore.h"



Semaphore::Semaphore(uint64_t count)
{   
    this -> count.value = count;
    
}

Semaphore::~Semaphore()
{
}

void Semaphore::__down(){

    current->state = TASK_UNINTERRUPTIBLE;
    waitList.push(current);
    current->flags |= NEED_SCHEDULE;
}
void Semaphore::down(){

    if(atomic_read(&count) > 0){
        atomic_dec(&count);
    }
    else{

        __down();

    }


}
void Semaphore::__up(){

     task* tsk  = waitList.pop();
     tsk->state = TASK_RUNNING;
     TASKMANAGER->addTaskToScheduleQueue(tsk);


}
void Semaphore::up(){


    if(waitList.isEmpty()){
        atomic_inc(&count);
    }else{
        __up();
    }

}
